import 'package:cbec_app/components/base/base_image.dart';
import 'package:cbec_app/components/base/base_interactive.dart';
import 'package:cbec_app/components/base/base_page.dart';
import 'package:cbec_app/components/common/common_style.dart';
import 'package:cbec_app/components/base/base_nav_bar.dart';
import 'package:cbec_app/entity/home/config_data.dart';
import 'package:cbec_app/pages/my/controller/withdraw_ctr.dart';
import 'package:cbec_app/routes/routes.dart';
import 'package:cbec_app/utils/ThemeConfig.dart';
import 'package:cbec_app/utils/Tools.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter/services.dart';

class WithDraw extends GetView<WithDrawController> {
  WithDraw({super.key});

  // final WithDrawController withdrawCtr = Get.put(WithDrawController());
  @override
  Widget build(BuildContext context) {
    return GetBuilder<WithDrawController>(builder: (controller) {
      return BasePage(
        title: "提现",
        iconType: IconType.back,
        children: [
          Container(
            margin: EdgeInsets.all(18.w),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.start,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                //到账账户
                if (controller.bankList.isNotEmpty) ...[
                  _top(context)
                ] else ...[
                  Container(
                    width: double.infinity,
                    padding: const EdgeInsets.only(left: 8),
                    decoration: BoxDecoration(border: Border.all(color: ThemeConfig.divideColor, width: 1)),
                    height: 68,
                    child: InkWell(
                      onTap: () {
                        Get.toNamed(Routes.bankCard);
                      },
                      child: Column(
                        mainAxisAlignment: MainAxisAlignment.spaceAround,
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [Text('到账账户', style: blackFont(14)), Text('去添加银行卡', style: TextStyle(fontSize: 16, fontWeight: FontWeight.w500, color: Tools.fromHex('#000000')))],
                      ),
                    ),
                  )
                ],
                //提现金额
                const SizedBox(
                  height: 10,
                ),
                Text(
                  "提现金额",
                  style: blackFont(14),
                ),
                const SizedBox(
                  height: 10,
                ),
                Container(
                  decoration: bottom_gary_border,
                  child: TextFormField(
                    decoration: descript("请输入提现金额",
                        icon: const Icon(
                          Icons.currency_yen,
                          size: 18,
                          weight: 900,
                        )),
                    keyboardType: const TextInputType.numberWithOptions(decimal: true),
                    inputFormatters: [
                      FilteringTextInputFormatter.allow(RegExp(r'^\d+\.?\d{0,2}')),
                    ],
                    controller: controller.textCtr,
                    onChanged: (value) => controller.onUserInputChanged(value),
                  ),
                ),
                Container(
                  margin: EdgeInsets.only(top: 10.w),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Text("可提现${controller.amount}元", style: blackFont(14)),
                      InkWell(
                        onTap: () => {controller.withDrawAll()},
                        child: Text(
                          "全部提现",
                          style: TextStyle(fontSize: 14, color: Tools.fromHex("#A34E3C")),
                        ),
                      )
                    ],
                  ),
                ),
                //按钮
                const SizedBox(
                  height: 100,
                ),
                Container(
                    alignment: Alignment.center,
                    child: ElevatedButton(
                        style: ButtonStyle(minimumSize: MaterialStateProperty.all<Size>(Size(339, 40)), backgroundColor: MaterialStateProperty.all(controller.buttonColors.value) //const Color.fromRGBO(248, 63, 45, 1)),这是可用状态的颜色
                            ),
                        onPressed: () => {controller.submit()},
                        //自带的按钮有阴影，先暂时用到
                        child: const Text(
                          "提交申请",
                          style: TextStyle(fontSize: 14.0, fontWeight: FontWeight.w500, color: Colors.white),
                        ))),
                Container(
                  margin: EdgeInsets.only(top: 10.w, left: 5.w),
                  child: Text(
                    "注:发起提现申请后，1-3个工作日到账",
                    style: TextStyle(fontSize: 12, color: Tools.fromHex("#727272")),
                  ),
                )
              ],
            ),
          ),
        ],
      );
    });
  }

  Widget _top(context) => InkWell(
        onTap: () => {
          showModalBottomSheet(
              context: context,
              builder: (BuildContext context) {
                return Container(
                    alignment: Alignment.topCenter,
                    padding: EdgeInsets.only(left: 18.w, right: 18.w, top: 10.w),
                    decoration: border_radis(Colors.white),
                    child: ListView.builder(
                        itemCount: controller.bankList.length,
                        itemBuilder: (context, index) {
                          final Map item = controller.bankList[index];
                          return bankItem(index, item["bankName"], icon: item['icon']);
                        }));
              })
        },
        child: Container(
            padding: EdgeInsets.all(8.w),
            decoration: BoxDecoration(
                border: Border.all(
              color: Color.fromRGBO(243, 245, 247, 1),
              width: 1.0,
            )),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              crossAxisAlignment: CrossAxisAlignment.end,
              children: [
                //左侧
                Container(
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(
                        "到账账户",
                        style: blackFont(13),
                      ),
                      SizedBox(
                        height: 10,
                      ),
                      Container(
                        child: Row(
                          children: [
                            ClipRRect(
                                borderRadius: BorderRadius.circular(50),
                                child: Image.network(
                                  controller.bankList[controller.currentBank]['icon'] == '' ? "default_avatar.png" : controller.bankList[controller.currentBank]['icon'],
                                  width: 26.w,
                                  height: 26,
                                )),
                            SizedBox(
                              width: 10.w,
                            ),
                            Text("${controller.bankList[controller.currentBank]['bankName']}(${controller.bankList[controller.currentBank]['bankCard']})", style: blackFont(13))
                          ],
                        ),
                      )
                    ],
                  ),
                ),
                //右侧
                Container(
                  child: Icon(
                    Icons.arrow_forward_ios,
                    size: 14,
                  ),
                )
              ],
            )),
      );

  Widget bankItem(int index, String name, {String icon = ''}) {
    return BaseInteractive(
      onClick: () => {controller.choiseBank(index), Get.back()},
      childWidget: Container(
        decoration: bottom_gary_border,
        padding: EdgeInsets.only(top: 10.w, bottom: 10.w),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.start,
          children: [BaseImage(url: icon == '' ? "default_avatar.png" : icon, width: 50, height: 50, borderRadius: BorderRadius.circular(25)), SizedBox(width: 30.w), Text(name)],
        ),
      ),
    );
  }
}
